/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdvancedQueryDialog.java
 *
 * Created on 16-feb-2011, 21:49:59
 */

package es.unavarra.mti.iws.gui;

import es.unavarra.mti.iws.Manager;
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.util.Iterator;

/**
 *
 * @author Marcelo
 */
public class AdvancedQueryDialog extends javax.swing.JDialog {
    private Manager manager;
    private Frame parent;
    /** Creates new form AdvancedQueryDialog */
    public AdvancedQueryDialog(Manager manager, java.awt.Frame parent) {
        super(parent, true);
        this.manager = manager;
        this.parent = parent;
        initComponents();
        pack();
    }

    public void setVisible(boolean b) {
        Rectangle parentBounds = this.parent.getBounds();
        Dimension size = getSize();
        // Center in the parent
        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
        super.setVisible(b);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        subjectCheckBox = new javax.swing.JCheckBox();
        predicateLabel = new javax.swing.JLabel();
        objectCheckBox = new javax.swing.JCheckBox();
        filtroLabel = new javax.swing.JLabel();
        subjectTextField = new javax.swing.JTextField();
        predicateComboBox = new javax.swing.JComboBox();
        objectTextField = new javax.swing.JTextField();
        filterTextField = new javax.swing.JTextField();
        conditionComboBox = new javax.swing.JComboBox();
        valueTextField = new javax.swing.JTextField();
        prepareButton = new javax.swing.JButton();
        queryLabel = new javax.swing.JLabel();
        queryScrollPane = new javax.swing.JScrollPane();
        queryTextArea = new javax.swing.JTextArea();
        resultsLabel = new javax.swing.JLabel();
        resultsScrollPane = new javax.swing.JScrollPane();
        resultsTable = new javax.swing.JTable();
        closeButton = new javax.swing.JButton();
        cleanButton = new javax.swing.JButton();
        executeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta Avanzada");

        subjectCheckBox.setFont(new java.awt.Font("Tahoma", 1, 11));
        subjectCheckBox.setSelected(true);
        subjectCheckBox.setText("Nodo");

        predicateLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        predicateLabel.setText("Propiedad");

        objectCheckBox.setFont(new java.awt.Font("Tahoma", 1, 11));
        objectCheckBox.setSelected(true);
        objectCheckBox.setText("Valor");

        filtroLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        filtroLabel.setText("Filtro");

        predicateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vecino", "Temperatura", "Humedad", "Inclinacion", "Tipo" }));

        conditionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ">", ">=", "<", "<=", "=" }));

        prepareButton.setText("Preparar");
        prepareButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prepareButtonActionPerformed(evt);
            }
        });

        queryLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        queryLabel.setText("Consulta SPARQL:");

        queryTextArea.setColumns(20);
        queryTextArea.setFont(new java.awt.Font("Courier New", 0, 12));
        queryTextArea.setRows(5);
        queryScrollPane.setViewportView(queryTextArea);

        resultsLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        resultsLabel.setText("Resultados:");

        resultsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nodo", "Valor"
            }
        ));
        resultsScrollPane.setViewportView(resultsTable);

        closeButton.setText("Cerrar");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        cleanButton.setText("Limpiar");
        cleanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanButtonActionPerformed(evt);
            }
        });

        executeButton.setText("Ejecutar");
        executeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                executeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(queryScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(subjectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(subjectCheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(predicateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(predicateLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(objectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(objectCheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(filtroLabel)
                            .addComponent(filterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addComponent(conditionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(valueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cleanButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(prepareButton)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(closeButton)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(queryLabel)
                        .addGap(491, 491, 491))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(resultsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(resultsLabel)
                        .addContainerGap(524, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(executeButton)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(predicateLabel)
                    .addComponent(filtroLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(objectCheckBox)
                    .addComponent(subjectCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(subjectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(predicateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(valueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(conditionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(objectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prepareButton)
                    .addComponent(cleanButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(queryLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(queryScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(executeButton)
                .addGap(4, 4, 4)
                .addComponent(resultsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resultsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(closeButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void prepareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prepareButtonActionPerformed
        manager.prepareAdvancedQuery(subjectCheckBox.isSelected(),
                objectCheckBox.isSelected(),
                subjectTextField.getText(),
                predicateComboBox.getSelectedItem().toString(),
                objectTextField.getText(),
                filterTextField.getText(),
                conditionComboBox.getSelectedItem().toString(),
                valueTextField.getText());
    }//GEN-LAST:event_prepareButtonActionPerformed

    public void subjectFocus() {
        subjectTextField.requestFocusInWindow();
    }
    
    public void objectFocus() {
        objectTextField.requestFocusInWindow();
    }

    public void setQueryValue(String query) {
        queryTextArea.setText(query);
    }

    public void setResults(List<List<String>> results) {
        String[] head = {"Nodo", "Valor"};
        DefaultTableModel dtm = new DefaultTableModel(head, results.size());

        Iterator list = results.iterator();

        int i = 0;
        while (list.hasNext()) {
            List<String> aux = results.get(i);
            dtm.setValueAt(aux.get(0), i, 0);
            dtm.setValueAt(aux.get(1), i, 1);
            list.next();
            i++;
        }
        
        resultsTable.setModel(dtm);
    }

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    public void cleanResults () {
        String[] head = {"Nodo", "Valor"};
        resultsTable.setModel(new DefaultTableModel(head, 4));
    }

    private void cleanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanButtonActionPerformed
        objectCheckBox.setSelected(true);
        subjectTextField.setText("");
        predicateComboBox.setSelectedIndex(0);
        objectTextField.setText("");
        filterTextField.setText("");
        conditionComboBox.setSelectedIndex(0);
        valueTextField.setText("");
        queryTextArea.setText("");
        cleanResults();


        String[] head = {"Nodo", "Valor"};
        resultsTable.setModel(new DefaultTableModel(head, 0));
    }//GEN-LAST:event_cleanButtonActionPerformed

    private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executeButtonActionPerformed
        manager.executeAdvancedQuery(queryTextArea.getText(), 
                subjectCheckBox.isSelected(), objectCheckBox.isSelected(),
                subjectTextField.getText(), objectTextField.getText());
    }//GEN-LAST:event_executeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cleanButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JComboBox conditionComboBox;
    private javax.swing.JButton executeButton;
    private javax.swing.JTextField filterTextField;
    private javax.swing.JLabel filtroLabel;
    private javax.swing.JCheckBox objectCheckBox;
    private javax.swing.JTextField objectTextField;
    private javax.swing.JComboBox predicateComboBox;
    private javax.swing.JLabel predicateLabel;
    private javax.swing.JButton prepareButton;
    private javax.swing.JLabel queryLabel;
    private javax.swing.JScrollPane queryScrollPane;
    private javax.swing.JTextArea queryTextArea;
    private javax.swing.JLabel resultsLabel;
    private javax.swing.JScrollPane resultsScrollPane;
    private javax.swing.JTable resultsTable;
    private javax.swing.JCheckBox subjectCheckBox;
    private javax.swing.JTextField subjectTextField;
    private javax.swing.JTextField valueTextField;
    // End of variables declaration//GEN-END:variables

}
